<?
session_start();
require_once "_mailing_libs.php";
check_session("/mailing/mailing_manager.php");

set_time_limit(10);

require_once "../lib/writeexcel/class.writeexcel_workbook.inc.php";
require_once "../lib/writeexcel/class.writeexcel_worksheet.inc.php";

$conn = mysql_connect("db.fittiche.com", "woodstock", "whoareyou") or die("Can't connect: " . mysql_error());
mysql_select_db("woodstock_prototype") or die("Can't select database");

$sql = "SELECT * FROM mailing_list WHERE status = 'active'";
$rs = mysql_query($sql) or die("SQL operation failed: " . mysql_error());


$fname = tempnam("/tmp", "woodstock_mailing.xls");
$workbook = &new writeexcel_workbook($fname);
$worksheet = &$workbook->addworksheet();

# The general syntax is write($row, $column, $token). Note that row and
# column are zero indexed
#

# Write some text
$worksheet->write(0, 0,  "email");
$worksheet->write(0, 1,  "name");
$worksheet->write(0, 2,  "join date");
$ridx = 0;

while ($row = mysql_fetch_row($rs)) {
	$ii = 0;
	$ridx++;
	$worksheet->write($ridx, $ii++, $row[1]);
	$worksheet->write($ridx, $ii++, $row[2]);
	$worksheet->write($ridx, $ii++, $row[4]);
}

$workbook->close();
mysql_free_result($rs);
mysql_close($conn);

header("Content-Type: application/x-msexcel; name=\"woodstock_mailing.xls\"");
header("Content-Disposition: inline; filename=\"woodstock_mailing.xls\"");
$fh=fopen($fname, "rb");
fpassthru($fh);
unlink($fname);
?>
